//
//  WP WPDiagnosisViewController.m
//  wobingwoyi
//
//  Created by WGP on 2016/11/30.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPDiagnosisViewController.h"
#import "WPDiagnosisHeaderView.h"
#import "WPMRModel.h"
#import "WPDiagnosisLabelCell.h"
#import "WPDiagnosisPictureCell.h"
#import "WPDiagnosisModel.h"
#import "WPReturnListModel.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "WPContentModel.h"
#import "WPSectionHeaderView.h"
#import "WPNoShowBigImageBtnCell.h"
#import "WPReturnSectionHeaderView.h"

@interface WPDiagnosisViewController ()<UITableViewDelegate,UITableViewDataSource,MWPhotoBrowserDelegate>

@property(nonatomic,strong)NSMutableArray *models;
@property(nonatomic,assign)BOOL isShowBigImage;
@property(nonatomic,strong)NSIndexPath *index;

@property(nonatomic,strong)NSMutableArray *MWPhotos;
@property(nonatomic,strong)NSMutableArray *allPicURLS;
@property(nonatomic,strong)NSArray *chinaNumArr;

@end

@implementation WPDiagnosisViewController

//友盟页面统计
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [MobClick beginLogPageView:[NSString stringWithUTF8String:object_getClassName(self)]];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:[NSString stringWithUTF8String:object_getClassName(self)]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.backgroundColor = GlobalBgColor;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    WPDiagnosisHeaderView *headerView = [WPDiagnosisHeaderView new];
    headerView.isExample = self.isExample;
    headerView.model = self.model;
    headerView.diseaseInstructionBlock = ^()
    {
        if (self.diseaseInstructionBlock)
        {
            self.diseaseInstructionBlock(self.model.name);
        }
    };
    self.tableView.tableHeaderView = headerView;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.allowsSelection = NO;
    
    [self.tableView registerClass:[WPDiagnosisLabelCell class] forCellReuseIdentifier:@"WPDiagnosisLabelCell"];
    [self.tableView registerClass:[WPDiagnosisPictureCell class] forCellReuseIdentifier:@"WPDiagnosisPictureCell"];
    [self.tableView registerClass:[WPNoShowBigImageBtnCell class] forCellReuseIdentifier:@"WPNoShowBigImageBtnCell"];
    [self.tableView registerClass:[WPSectionHeaderView class] forHeaderFooterViewReuseIdentifier:@"WPSectionHeaderView"];
     [self.tableView registerClass:[WPReturnSectionHeaderView class] forHeaderFooterViewReuseIdentifier:@"WPReturnSectionHeaderView"];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

-(void)imagePickerView:(NSIndexPath *)indexPath
{
    // Modal
    MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:browser];
    nc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    
    browser.displayActionButton = NO;
    browser.displayNavArrows = YES;
    browser.alwaysShowControls = YES;
    [browser setCurrentPhotoIndex:indexPath.item];
    
    [self presentViewController:nc animated:YES completion:nil];
}

#pragma mark - MWPhotoBrower的代理方法
//返回图片个数
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser{
    return self.MWPhotos.count;
}

//返回图片模型
- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index{
    
    if (index < self.MWPhotos.count)
    {
        return [self.MWPhotos objectAtIndex:index];
    }
    return nil;
    
}


#pragma mark - UITableView的数据源代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.models.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    WPDiagnosisModel *model = self.models[section];
    return model.textAndPicArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WPDiagnosisModel *model = self.models[indexPath.section];
    WPContentModel *contentModel = model.textAndPicArr[indexPath.item];
    
    if ([contentModel.content hasPrefix:@"http://"])
    {
        WPDiagnosisPictureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WPDiagnosisPictureCell"];
        
        cell.refreshCell = ^(NSIndexPath *indexPath){
        
            [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];

        };
        
        cell.showAllPicture = ^(NSString *url){
            
            [self.allPicURLS enumerateObjectsUsingBlock:^(NSString  *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                if ([obj isEqualToString:url])
                {
                    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:idx inSection:0];
                    [self imagePickerView:indexPath];
                    
                    *stop = YES;
                }
            }];
            
            
        };
        
        cell.showAllBigPicture = ^{
        
            for (WPDiagnosisModel *diagnosisModel in self.models)
            {
                for (WPContentModel *contentModel in diagnosisModel.textAndPicArr)
                {
                    if (contentModel.scale)
                    {
                        contentModel.width = SCREEN_WIDTH-2*10;
                        contentModel.height = contentModel.width/contentModel.scale;
                    }
                    else
                    {
                        //图片还没有加载出来的时候
                    }
                }
            }
            
            [self.tableView reloadData];
        };
        
        cell.model = contentModel;
        cell.url = contentModel.content;
        
        if (!self.isShowBigImage)
        {
            cell.isShowBigImage = YES;
            self.isShowBigImage = YES;
            self.index = indexPath;
        }
        else
        {
            cell.isShowBigImage = NO;
            if (self.index.section == indexPath.section && self.index.item == indexPath.item)
            {
                cell.isShowBigImage = YES;
            }
        }
        
        cell.indexPath = indexPath;
        
        return cell;
    }
    else
    {
        WPDiagnosisLabelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WPDiagnosisLabelCell"];
        cell.model = contentModel;
        cell.str = contentModel.content;
        return cell;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    WPDiagnosisModel *model = self.models[indexPath.section];
    WPContentModel *contentModel = model.textAndPicArr[indexPath.item];
    
    if ([contentModel.content hasPrefix:@"http://"])
    {
        return contentModel.height;
    }
    else
    {
        CGSize size = [contentModel.content sizeWithMaxSize:CGSizeMake(SCREEN_WIDTH-2*10, CGFLOAT_MAX) font:14];
        return size.height+2 + 30;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        WPSectionHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"WPSectionHeaderView"];
        WPDiagnosisModel *model = self.models[section];
        
        headerView.time = [NSString stringWithFormat:@"%@诊时间:  %@",self.chinaNumArr[section],model.time];

       headerView.isExample = self.isExample;
        return headerView;
    }
    else
    {
        WPReturnSectionHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"WPReturnSectionHeaderView"];
        
//        WPDiagnosisModel *model = self.models[section];
        headerView.section = section;
        headerView.model = self.model;
        
        return headerView;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 0)
    {
        return 44;
    }
    else
    {
        UIImage *ZDImage = [UIImage imageNamed:@"zhenduan"];
        return 220 + ZDImage.size.height+30;
    }
}

-(void)setModel:(WPMRModel *)model
{
    _model = model;
    
    WPDiagnosisModel *firstModel = [WPDiagnosisModel initWithTime:model.treatmentTime arr:model.recordDescp_list];
    [self.models addObject:firstModel];
    
    //首诊图片
    for (NSString *url in model.firstOptionUrls)
    {
        MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:url]];
        [self.MWPhotos addObject:photo];
        
        [self.allPicURLS addObject:url];
    }
    
    if (model.returnVisit_list.count)
    {
        for (WPReturnListModel *liseModel in model.returnVisit_list)
        {
            [self.models addObject:[WPDiagnosisModel initWithTime:liseModel.treatmentTime arr:liseModel.descp_list]];
            
            if(liseModel.descpURLS.count)
            {
                for (NSString *url in liseModel.descpURLS)
                {
                    MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:url]];
                    [self.MWPhotos addObject:photo];
                    
                    [self.allPicURLS addObject:url];
                }
            }
        }
    }
    
    
}

-(NSArray *)chinaNumArr
{
    if (_chinaNumArr == nil)
    {
        _chinaNumArr = [NSArray arrayWithObjects:@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"十", nil];
    }
    return _chinaNumArr;
}

-(NSMutableArray *)MWPhotos
{
    if (_MWPhotos == nil)
    {
        _MWPhotos = [NSMutableArray array];
    }
    return _MWPhotos;
}

-(NSMutableArray *)allPicURLS
{
    if (_allPicURLS == nil)
    {
        _allPicURLS = [NSMutableArray array];
    }
    return _allPicURLS;
}

-(NSMutableArray *)models
{
    if (_models == nil)
    {
        _models = [NSMutableArray array];
    }
    return _models;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
